package com.ganwei.mybatisplus.system.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ganwei.mybatisplus.config.util.ExcelImportUtils;
import com.ganwei.mybatisplus.config.util.PageData;
import com.ganwei.mybatisplus.config.util.ResultVo;
import com.ganwei.mybatisplus.system.pojo.Users;
import com.ganwei.mybatisplus.system.service.UsesService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/users")
@Slf4j
public class UsersController {

    @Resource
    private UsesService usesService;

    /**
     * 不使用mybatisplus分页，多表查询时，mysql自带分页是不适用的
     */
    @GetMapping("/queryUsersAll")
    public PageData<Users> queryUsersAll(Integer pageNum , Integer pageSize){
        PageData<Users> usersPageData = usesService.queryUsersPage( pageNum ,  pageSize);
        return  usersPageData;
    }

    @GetMapping("/pageUsers")
    public ResultVo pageUsers(@Valid @NotNull(message = "pageNum不能为空") Integer pageNum ,
                              @Valid @NotNull(message = "pageSize不能为空") Integer pageSize){
        try{
            PageData<Users> usersPageData = usesService.queryUsersPage(pageNum, pageSize);
            return ResultVo.success(200,"查询成功",usersPageData);
        }catch (Exception e){
            return ResultVo.error(500,"查询失败",null);
        }

    }

    @PostMapping("/login")
    public ResultVo login(@RequestBody JSONObject login){
        String username = login.getString("username");
        String password = login.getString("password");
        if(!username.equals("ganwei") || !password.equals("ganwei11!!")){
            return ResultVo.error(500,"登录失败"+"帐号或者密码不对",login);
        }
        return ResultVo.success(200,"登录成功",login);
    }

    /**
     * 导出
     *
     * @param response
     */
    @GetMapping("/queryUsersAllexport")
    public void exportExcel(HttpServletResponse response) throws IOException {

        List<Users> usersPageData = usesService.queryAll(response);
    }

    @PostMapping("/updateUsers")
    public ResultVo updateUsers(@Valid @RequestBody Users users){
        System.out.println(users);
        int i = usesService.updateUser(users);
        return i==1? ResultVo.success(200,"修改成功",users)
                :ResultVo.error(500,"修改失败",users);
    }


    /**
     * 查询全部
     *
     */
    @GetMapping("/userAll/{deleted}")
    public ResultVo userAll(@PathVariable("deleted")int deleted) throws IOException {
        QueryWrapper<Users> wrapper = new QueryWrapper<>();
        wrapper.eq("deleted","deleted");
        List<Users> usersPageData = usesService.userAll(wrapper);
        return ResultVo.success(200,"查询成功",usersPageData);
    }


    /**
     * 查询全部
     *
     */
    @GetMapping("/userById/{id}")
    public ResultVo userById(@PathVariable("id")Long id) throws IOException {
        try {
            List list = new ArrayList();
            Users userById = usesService.userById(id);
            list.add(userById);
            return ResultVo.success(200, "查询成功", list);
        }catch (Exception e){
            return ResultVo.error(500,"查询失败",null);
        }
    }

    /**
     *
     * @Title: impUser
     * @Description: excle导入
     * @param file
     * @return String
     */
    @PostMapping("/impUser")
    public String impUser(MultipartFile file){
        List<Users> users = ExcelImportUtils.importData(file, 1, Users.class);
        boolean result = usesService.insertAll(users);

        return "success";
    }

    @PostMapping("/addUser")
    public ResultVo addUser(@Valid @RequestBody Users users){
        int i = usesService.usersSave(users);
        return i==1? ResultVo.success(200,"新增成功",users)
                :ResultVo.error(500,"添加失败",users);
    }

//    @GetMapping("/dynamicMenu")
//    public List<Map> dynamicMenu(){
//        List list = new ArrayList();
//        list =
//    }

}
